package sing.core.dialog

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import sing.core.R
import sing.core.ext.view.clickNoRepeat
import sing.core.listener.OnItemClickListener
import sing.core.network.bean.IdNameValueBean

class BottomSheetListDialog(var mContext: Context,val list: MutableList<IdNameValueBean>, var listener: OnClickListener) : BottomSheetDialog(mContext, R.style.BottomSheetDialogStyle) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val view = LayoutInflater.from(mContext).inflate(R.layout.dialog_bottom_sheet_list, null, false)
        setCancelable(true)
        setContentView(view) //设置对框框中的布局

//        window?.findViewById<View>(android.R.id.design_bottom_sheet)?.setBackgroundResource(android.R.color.transparent)
//
//        behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
//            override fun onStateChanged(@NonNull bottomSheet: View, newState: Int) {
//                if (newState == BottomSheetBehavior.STATE_DRAGGING) {   //禁止拖拽，设置为收缩状态
//                    behavior.state = BottomSheetBehavior.STATE_COLLAPSED
//                }
//            }
//            override fun onSlide(@NonNull bottomSheet: View, slideOffset: Float) {}
//        })
////        设置弹出高度
//        behavior.peekHeight = 350

        val recyclerView = view.findViewById<RecyclerView>(R.id.recycler_view)
        recyclerView.adapter = MyAdapter(list,object :OnItemClickListener<IdNameValueBean>{
            override fun onItemClick(data: IdNameValueBean, position: Int, type: Int) {
                dismiss()
                listener.result(data)
            }
        })
    }

    override fun show() {
        super.show()
    }

    interface OnClickListener {
        fun result(data: IdNameValueBean)
    }

    class MyAdapter(val list: MutableList<IdNameValueBean>,val listener: OnItemClickListener<IdNameValueBean>) : BaseQuickAdapter<IdNameValueBean, BaseViewHolder>(R.layout.item_bottom_sheet,list){
        override fun convert(holder: BaseViewHolder, item: IdNameValueBean) {
            if (holder.layoutPosition == list.size - 1){
                holder.setGone(R.id.view,true)
            }else{
                holder.setVisible(R.id.view,true)
            }

            holder.setText(R.id.tv_str,item.name)
            holder.itemView.clickNoRepeat { listener.onItemClick(item,holder.layoutPosition,0) }
        }
    }
}